package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

@SpringBootTest
public class testZset {

    @Autowired
    private RedisTemplate<String,String> redisTemplate; // redisTemplate是springboot自动注入的


    /**
     * 操作有序集合类型的数据
     */
    @Test
    public void testZset(){
        //添加数据
        redisTemplate.opsForZSet().add("myzset","a",1);
        redisTemplate.opsForZSet().add("myzset","b",10);
        redisTemplate.opsForZSet().add("myzset","c",20);
        System.out.println("===================================");
        //获取集合中的所有成员
        Set<String> members = redisTemplate.opsForZSet().range("myzset", 0, -1);
        System.out.println(members);
        System.out.println("===================================");
        //给a成员的分数增加10
        redisTemplate.opsForZSet().incrementScore("myzset","a",10);
        System.out.println("===================================");
        //删除a、b两个成员
        redisTemplate.opsForZSet().remove("myzset","a","b");
    }
}
